package com.lemon.smartsafety.http.service;

import android.content.Context;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.common.CommonVo;
import com.lemon.smartsafety.entity.safe.inspection.CompanyBuildingItemCheckInfoVo;
import com.lemon.smartsafety.entity.safe.inspection.CompanyBuildingItemFlowVo;
import com.lemon.smartsafety.entity.safe.inspection.FireCheckLastDetailVo;
import com.lemon.smartsafety.entity.safe.inspection.FireCheckRecordVo;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.orhanobut.logger.Logger;
import com.zhy.http.okhttp.OkHttpUtils;


/**
 * Created by Cooliean on 16/1/19.
 * 安全巡查 相关 网络请求服务
 */
public class InspectionServices extends BaseHttpServices {

    private LoggerHelper loggerHelper = new LoggerHelper(InspectionServices.class);


    private static final String TAG = "InspectionServices";


    private InspectionServices(Context context) {
        super(context);
    }

    public static InspectionServices init() {
        return new InspectionServices(IApplication.context());
    }


    /**
     * 获取检查项目的检查流程数据
     *
     * @return
     */
    public InspectionServices getCheckItemFlowList(String itemId, String checkId) {
        LogUtils.LOGI("CheckItemFlowList_checkId",checkId);
        LemonRequestParams params = new LemonRequestParams();
        params.put("item_id", StringUtils.nullValue(itemId));
        params.put("check_id", StringUtils.nullValue(checkId));
        LogUtils.LOGI("CheckItemFlowListDetail_params",params.toString());
        return getCheckItemFlowList(params);
    }


    /**
     * 获取检查项目的检查流程数据
     *
     * @param params
     * @return
     */
    private InspectionServices getCheckItemFlowList(LemonRequestParams params) {
        HandlerCallback<CompanyBuildingItemCheckInfoVo> callback = new HandlerCallback<CompanyBuildingItemCheckInfoVo>(EventTag.TAG_INSPECTION_CHECK_ITEM_FLOW) {
        };
        callback.setOther(RefreshType.REFRESH);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.URL_INSPECTION_CHECK_ITEM_FLOW);
        Logger.i("serverUrl__CheckItemFlow\n"+serverUrl);
        Logger.i("params__CheckItemFlow\n"+params.toString());
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


    /**
     * 获取检查项目的检查流程数据
     *
     * @return
     */
    public InspectionServices getCheckItemFlowListDetail(String flowId, String checkId) {
        LogUtils.LOGI("CheckItemFlowListDetail_checkId",checkId);
        LemonRequestParams params = new LemonRequestParams();
        params.put("flow_id", StringUtils.nullValue(flowId));
        params.put("check_id", StringUtils.nullValue(checkId));
        LogUtils.LOGI("CheckItemFlowListDetail_params",params.toString());
        return getCheckItemFlowListDetail(params);
    }


    /**
     * 获取检查项目的检查流程数据
     *
     * @param params
     * @return
     */
    private InspectionServices getCheckItemFlowListDetail(LemonRequestParams params) {
        HandlerCallback<CompanyBuildingItemFlowVo> callback = new HandlerCallback<CompanyBuildingItemFlowVo>(EventTag.TAG_INSPECTION_CHECK_ITEM_FLOW_CONCLUSION) {
        };
        callback.setOther(RefreshType.REFRESH);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.URL_INSPECTION_CHECK_ITEM_FLOW_CONCLUSION);
        Logger.i("serverUrl__CheckItemFlowDetail\n"+serverUrl);
        Logger.i("params__CheckItemFlowDetail\n"+params.toString());
        OkHttpUtils.getInstance().post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


    /**
     * 保存巡查详细
     *
     * @return
     */
    public InspectionServices saveInspectionDetail(LemonRequestParams params, boolean isEdit) {
        HandlerCallback<CommonVo> callback = new HandlerCallback<CommonVo>(EventTag.TAG_INSPECTION_CHECK_ITEM_FLOW_ACTION) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(isEdit ? UrlInfo.URL_INSPECTION_CHECK_DETAIL_EDIT : UrlInfo.URL_INSPECTION_CHECK_DETAIL_NEW);
        Logger.i(serverUrl);
        Logger.i(params.toString());
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


    /**
     * 保存巡查情况
     *
     * @return
     */
    public InspectionServices saveInspectionCheck(LemonRequestParams params) {
        HandlerCallback<String> callback = new HandlerCallback<String>(EventTag.TAG_INSPECTION_CHECK_ACTION) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.URL_INSPECTION_CHECK_ACTION);
        Logger.i("serverUrl\n"+serverUrl);
        Logger.i("params\n"+params.toString());
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


    /**
     * 获取检查项目详细历史记录
     *
     * @return
     */
    public InspectionServices getInspectionCheckItemRecord(String itemId) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("item_id", StringUtils.nullValue(itemId));

        HandlerCallback<FireCheckRecordVo> callback = new HandlerCallback<FireCheckRecordVo>(EventTag.TAG_INSPECTION_CHECK_ITEM_RECORD) {
        };
        callback.setOther(RefreshType.REFRESH);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.URL_INSPECTION_CHECK_ITEM_RECORD);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


    /**
     * 获取检查项目的最后一次详情
     *
     * @return
     */
    public InspectionServices getInspectionCheckItemDetail(String itemId) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("item_id", StringUtils.nullValue(itemId));

        HandlerCallback<FireCheckLastDetailVo> callback = new HandlerCallback<FireCheckLastDetailVo>(EventTag.TAG_INSPECTION_CHECK_ITEM_DETAIL) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.URL_INSPECTION_CHECK_ITEM_DETAIL);
        Logger.i(serverUrl);
        Logger.i(params.toString());
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


    /**
     * 提交报修
     *
     * @return
     */
    public InspectionServices saveCheckItemRepair(LemonRequestParams params) {
        HandlerCallback<String> callback = new HandlerCallback<String>(EventTag.TAG_INSPECTION_CHECK_ITEM_REPAIR) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.URL_INSPECTION_CHECK_ITEM_REPAIR_ACTION);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }

}
